package com.ruoyi.syf.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.annotation.ConvertField;
import com.ruoyi.common.annotation.ConvertFieldItem;
import com.ruoyi.syf.common.constant.Check;
import com.ruoyi.common.enums.ConvertType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.syf.domain.RiskCheckDangerDetect;
import com.ruoyi.syf.domain.RiskCheckDangerFix;
import com.ruoyi.syf.domain.vo.RiskCheckDangerDetectVo;
import com.ruoyi.syf.mapper.RiskCheckDangerDetectMapper;
import com.ruoyi.syf.mapper.RiskCheckDangerFixMapper;
import com.ruoyi.syf.service.IRiskCheckDangerDetectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 隐患整改验收Service业务层处理
 *
 * @author yr
 * @date 2024-08-03
 */
@Service
public class RiskCheckDangerDetectServiceImpl extends ServiceImpl<RiskCheckDangerDetectMapper,RiskCheckDangerDetect> implements IRiskCheckDangerDetectService {
    @Autowired
    private RiskCheckDangerDetectMapper riskCheckDangerDetectMapper;
    @Autowired
    private RiskCheckDangerFixMapper riskCheckDangerFixMapper;

    @ConvertField({
            @ConvertFieldItem(fieldName = "detectBy", assistFieldName = "detectByName", type = ConvertType.USER),
            @ConvertFieldItem(fieldName = "actualDetectBy", assistFieldName = "actualDetectByName", type = ConvertType.USER)
    })
    @Override
    public List<RiskCheckDangerDetectVo> list(RiskCheckDangerDetect riskCheckDangerDetect) {
        riskCheckDangerDetect.setLoginUserId(SecurityUtils.getUserId());
        return riskCheckDangerDetectMapper.list(riskCheckDangerDetect);
    }

    @ConvertField({
            @ConvertFieldItem(fieldName = "actualDetectBy", assistFieldName = "actualDetectByName", type = ConvertType.USER)
    })
    @Override
    public RiskCheckDangerDetectVo getInfo(Long id) {
        return riskCheckDangerDetectMapper.getInfo(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean await(RiskCheckDangerDetect riskCheckDangerDetect) {
        if (riskCheckDangerDetect.getActualDetectBy() == null){
            riskCheckDangerDetect.setActualDetectBy(SecurityUtils.getUserId());
        }
        //回退
        if (riskCheckDangerDetect.getDetectStatus().equals(Check.CheckInfo.E_BACK.getCode())){
            RiskCheckDangerFix dangerFix = riskCheckDangerFixMapper.selectById(riskCheckDangerDetect.getFixId());
            dangerFix.setFixStatus(Check.CheckInfo.F_TO_CONFIRM.getCode());
            return riskCheckDangerFixMapper.updateById(dangerFix)> 0;
        }else {
            return updateById(riskCheckDangerDetect);
        }
    }

    @Override
    public int deleteRiskCheckDangerDetectByIds(Long[] ids) {
        return riskCheckDangerDetectMapper.deleteRiskCheckDangerDetectByIds(ids);
    }

    @Override
    public int deleteRiskCheckDangerDetectById(Long id) {
        return riskCheckDangerDetectMapper.deleteRiskCheckDangerDetectById(id);
    }

    @Override
    public boolean mySave(RiskCheckDangerFix fix) {
        RiskCheckDangerDetect detect = new RiskCheckDangerDetect();
        detect.setDangerId(fix.getDangerId());
        detect.setFixId(fix.getId());
        detect.setDetectStatus(Check.CheckInfo.E_AWAIT.getCode());
        return save(detect);
    }

}
